<?php
namespace Home\Common;
use Think\Controller;
class Common extends Controller{

    private $_listprid;

    public function _initialize(){

        if(ACTION_NAME!='login' && ACTION_NAME!='out' && ACTION_NAME!='verify'){
            if(empty($_SESSION['aid'])){//判断是否登录
                $this->error('请登录',U('index/login'));
            }
            $info=M('staffinfo')->where('stf_id='.$_SESSION['aid'])->join('left join postinfo on staffinfo.pt_id=postinfo.pt_id')->find();
            if(session_id()!=$info['daid']){
                if($_SESSION['aid']){
                    M('staffinfo')->where('stf_id='.$_SESSION['aid'])->save(array('md5'=>''));
                }
                $info['adb']=$info['adb']+1;
                M('staffinfo')->where('stf_id='.$_SESSION['aid'])->save(array('adb'=>$info['adb']));

                unset($_SESSION['aid']);
                cookie('aid',null);
                cookie('amd5',null);
                $this->error('账号异地登录！',U('index/login'));
            }

            $this->_listprid=$info;
            if (empty($this->_listprid['pt_id'])){
                $this->_listprid['pt_id'] = 0;
                $this->_listprid['pt_name'] = '站长';
            }
            $this->assign('_listprid',$this->_listprid);

            $this->assign('adminifo',$info);
            $this->checkAuth($info['pt_id']);
        }
    }

    private function checkAuth($pt_id)
    {
        if ($pt_id != 0) {//判断是否有登录的账号
            $auth = M('auth');
            //查询此号拥有的权限
            $state = $auth->join('menu on menu.mid=auth.mid')->where('`pt_id`=' . $pt_id . ' and `state`=1 and `controll`="' . strtolower(CONTROLLER_NAME) . '" and `view`="' . strtolower(ACTION_NAME) . '"')->select();
            if (empty($state)) {//判断是否有此权限
                $this->error('您没有权限访问该功能');
            } else {
                $menu = $auth->join('menu on menu.mid=auth.mid')->where('`hidden`=1 and `pt_id`=' . $pt_id . ' and `state`=1')->select();
                $this->assign('menutop', $menu);
            }
        } else {
            $menu = M('menu');
            $menu = $menu->where('`hidden`=1')->select();
            $this->assign('menutop', $menu);

        }
    }
}